
package com.eworking.eim.receive;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;

import com.eworking.eim.EIMChatManager;
import com.eworking.eim.group.EIMGroup;
import com.eworking.eim.group.EIMGroupChangeListener;
import com.eworking.eim.group.EIMGroupManager;
import com.eworking.eim.user.EIMAccountUtils;
import com.eworking.eim.util.EIMLog;

public class EIMMUCPresenceListener implements PacketListener {
    
    /** 日志输出标记 */
    private static final String _TAG = EIMMUCPresenceListener.class.getSimpleName();
    
    private static final String _MUC_ELEMENT_NAME = "x";
    
    private static final String _MUC_NS_USER = "http://jabber.org/protocol/muc#user";
    
    private static final String _ITEM_DESTROY = "destroy";
    
    private static final String _ITEM_EXITMUC = "<item affiliation=\"none\" role=\"none\">";
    
    public synchronized void processPacket(Packet packet) {
        try {
            Presence presence = (Presence) packet;
            if (presence.getType() != Presence.Type.unavailable) {
                return;
            }
            PacketExtension packetExtension = presence.getExtension(_MUC_ELEMENT_NAME, _MUC_NS_USER);
            if (packetExtension != null) {
                String action = packetExtension.toXML();
                if (action.contains(_ITEM_DESTROY)) {
                    // 删除群组
                    handleRoomDestroy(packet.getFrom());
                } else if (action.contains(_ITEM_EXITMUC)) {
                    // 删除成员
                    handleUserRemove(packet.getFrom());
                }
            }
        } catch (Exception e) {
            EIMLog.e(_TAG, "群组操作监听处理失败：" + e.getMessage());
        }
    }
    
    /**
     * 从XMPP服务端删除群组
     * 
     * @param roomID
     */
    private void handleRoomDestroy(String roomID) {
        String groupId = EIMAccountUtils.getGroupIdFromGroupJID(roomID);
        EIMGroup group = EIMGroupManager.getInstance().allGroups.get(groupId);
        String groupName = "";
        if (group != null) {
            groupName = group.getGroupName();
        }
        EIMLog.d(_TAG, "从XMPP服务端删除群组：" + groupId + " name:" + groupName);
        EIMGroupManager.getInstance().deleteGroupClient(groupId);
        
        // 从XMPP服务端销毁群组
        for (EIMGroupChangeListener groupChangeListener : EIMGroupManager.getInstance().allGroupChangeListeners) {
            groupChangeListener.onGroupDestroy(groupId, groupName);
        }
    }
    
    /**
     * 删除群组的成员
     * 
     * @param fullJID
     */
    private void handleUserRemove(String fullJID) {
        int index = fullJID.indexOf("/");
        if (index > 0) {
            String account = fullJID.substring(index + 1);
            String groupJID = fullJID.substring(0, index - 1);
            String groupId = EIMAccountUtils.getGroupIdFromGroupJID(groupJID);
            if (account.equals(EIMChatManager.getInstance().getCurrentAccount())) {
                EIMLog.d(_TAG, "该用户从群组中删除：" + account);
                String groupName = "";
                EIMGroup group = EIMGroupManager.getInstance().allGroups.get(groupId);
                if (group != null) {
                    groupName = group.getGroupName();
                }
                EIMGroupManager.getInstance().deleteGroupClient(groupId);
                // 删除群组中的成员
                for (EIMGroupChangeListener groupChangeListener : EIMGroupManager.getInstance().allGroupChangeListeners) {
                    groupChangeListener.onUserRemoved(groupId, groupName);
                }
                
            }
        }
    }
}
